package net.jdm.constraint.cycle;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import net.jdm.common.Alert;
import net.jdm.common.Alert.AlertLevel;
import net.jdm.common.Alert.DetailLevel;
import net.jdm.constraint.Constraint;
import net.jdm.domain.entity.ContainerNode;
import net.jdm.domain.entity.Scope;

public class CycleConstraint implements Constraint{
    protected int thresholdLenght=1;
    protected Alert alert=new Alert();
    private CyclePrinter cyclePrinter;
    public CycleConstraint(int thresholdLenght){
		this.thresholdLenght=thresholdLenght;
	}
	public boolean check(Scope crycleScope) {
	    boolean result=true;
	    Set<List<? extends ContainerNode>> cycles=null;
	   	cycles=getAllOversizeCycles(crycleScope.getCyclesWithinMe());
	   	if(!cycles.isEmpty()){
	   	   result=false;
	   	   getPrinter().printCyclesInfo(crycleScope.getName(),cycles,alert,thresholdLenght);
	   	}
	   	return result;
	}
	public Set<List<? extends ContainerNode>> getAllOversizeCycles(Collection<List<? extends ContainerNode>> cycles) {
		Set<List<? extends ContainerNode>> result = new HashSet<List<? extends ContainerNode>>();
			for (List<? extends ContainerNode> cycle : cycles) {
				if (!cycle.isEmpty()&&cycle.size()>=thresholdLenght+2) {
					result.add(cycle);
				}
		}
		return result;
	}
	public int getThresholdLenght() {
		return thresholdLenght;
	}
	public void setThresholdLenght(int thresholdLenght) {
		this.thresholdLenght = thresholdLenght;
	}
	@Override
	public void setAlertLevel(AlertLevel alertLevel) {
		alert.setAlertLevel(alertLevel);
	}
	@Override
	public void setDetailLevel(DetailLevel detailLevel) {
		this.alert.setDetailLevel(detailLevel);
	}
	@Override
	public void setAlertMessage(String alertMessage) {
		this.alert.setAlertMessage(alertMessage);
	}
	public CyclePrinter getPrinter() {
		return cyclePrinter;
	}
	public void setPrinter(CyclePrinter cyclePrinter) {
		this.cyclePrinter = cyclePrinter;
	}
}
